package data;

import domain.Comment;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

public class JDBCCommentDAO implements ICommentDAO{

	public List<Comment> selectAllComments() {
		Connection conn = ConnectionManager.getInstance().checkOut();

        PreparedStatement stmt = null;
        List<Comment> searchResults = new LinkedList<Comment>();
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM Comments";
            stmt = conn.prepareStatement(sql);
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
                Comment temp = new Comment();
                temp.setId(result.getInt("id"));
                temp.setComment(result.getString("comment"));          
                temp.setDate(result.getDate("date"));
                searchResults.add(temp);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {}
           
         
        }   
        return searchResults;
	}

	public Comment select(Connection conn, String FOID) {
		PreparedStatement stmt = null;
        ResultSet result = null;
        Comment c = null;
        String sql = "SELECT * FROM comments WHERE (FOID = ?) ";

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, FOID);
            result = stmt.executeQuery();

            result.next();
            c = new Comment();
            c.setComment(result.getString("comment"));
            c.setDate(result.getDate("date"));
            c.setId(result.getInt("id"));
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return c;
	}

	public String selectCommentOID(Connection conn, String id) {
		PreparedStatement stmt = null;
        ResultSet result = null;
        String oidp = null;
        String sql = "SELECT * FROM Comments WHERE (FOID = ?) ";

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, id);
            result = stmt.executeQuery();

            result.next();
            oidp = result.getString("OID");
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return oidp;
	}

}

